
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.merchant.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.merchant.domain.MerMerchantApply;
import com.lxk.merchant.domain.bo.MerMerchantApplyBo;
import com.lxk.merchant.domain.vo.MerMerchantApplyVo;
import com.lxk.merchant.mapper.MerMerchantApplyMapper;
import com.lxk.merchant.service.IMerMerchantApplyService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商户申请Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-09-18
 */
@RequiredArgsConstructor
@Service
public class MerMerchantApplyServiceImpl implements IMerMerchantApplyService {

    private final MerMerchantApplyMapper baseMapper;

    /**
     * 查询IdList商户申请Map
     */
    @Override
    public Map<Long, MerMerchantApplyVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(MerMerchantApplyVo::getId, v -> v));
    }

    /**
     * 查询商户申请Map
     */
    @Override
    public Map<Long, MerMerchantApplyVo> queryMap(MerMerchantApplyBo bo) {
        return this.queryList(bo).stream().collect(Collectors.toMap(MerMerchantApplyVo::getId, v -> v));
    }

    /**
     * 查询商户申请IdList
     */
    @Override
    public List<MerMerchantApplyVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<MerMerchantApply> lqw = Wrappers.lambdaQuery();
        lqw.in(MerMerchantApply::getId, idList);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询商户申请
     *
     * @param id 主键
     * @return 商户申请
     */
    @Override
    public MerMerchantApplyVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询商户申请列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商户申请分页列表
     */
    @Override
    public TableDataInfo<MerMerchantApplyVo> queryPageList(MerMerchantApplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MerMerchantApply> lqw = buildQueryWrapper(bo);
        Page<MerMerchantApplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商户申请列表
     *
     * @param bo 查询条件
     * @return 商户申请列表
     */
    @Override
    public List<MerMerchantApplyVo> queryList(MerMerchantApplyBo bo) {
        LambdaQueryWrapper<MerMerchantApply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MerMerchantApply> buildQueryWrapper(MerMerchantApplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MerMerchantApply> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(MerMerchantApply::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null, MerMerchantApply::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null, MerMerchantApply::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.like(StringUtils.isNotBlank(bo.getMerchantName()), MerMerchantApply::getMerchantName, bo.getMerchantName());
        lqw.like(StringUtils.isNotBlank(bo.getContactPersonName()), MerMerchantApply::getContactPersonName, bo.getContactPersonName());
        lqw.like(StringUtils.isNotBlank(bo.getContactPhoneNumber()), MerMerchantApply::getContactPhoneNumber, bo.getContactPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantType()), MerMerchantApply::getMerchantType, bo.getMerchantType());
        lqw.eq(StringUtils.isNotBlank(bo.getEmailAddress()), MerMerchantApply::getEmailAddress, bo.getEmailAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessScope()), MerMerchantApply::getBusinessScope, bo.getBusinessScope());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisteredAddress()), MerMerchantApply::getRegisteredAddress, bo.getRegisteredAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessLicenseNumber()), MerMerchantApply::getBusinessLicenseNumber, bo.getBusinessLicenseNumber());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), MerMerchantApply::getBankName, bo.getBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAccountNumber()), MerMerchantApply::getBankAccountNumber, bo.getBankAccountNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getLegalRepresentative()), MerMerchantApply::getLegalRepresentative, bo.getLegalRepresentative());
        lqw.eq(bo.getRegisteredCapital() != null, MerMerchantApply::getRegisteredCapital, bo.getRegisteredCapital());
        lqw.eq(bo.getEstablishmentDate() != null, MerMerchantApply::getEstablishmentDate, bo.getEstablishmentDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMailingAddress()), MerMerchantApply::getMailingAddress, bo.getMailingAddress());
        lqw.eq(bo.getApplicationDate() != null, MerMerchantApply::getApplicationDate, bo.getApplicationDate());
        lqw.eq(bo.getUserId() != null, MerMerchantApply::getUserId, bo.getUserId());
        lqw.eq(bo.getAuditStatus() != null, MerMerchantApply::getAuditStatus, bo.getAuditStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessLicensePic()), MerMerchantApply::getBusinessLicensePic, bo.getBusinessLicensePic());
        lqw.eq(StringUtils.isNotBlank(bo.getDenialReason()), MerMerchantApply::getDenialReason, bo.getDenialReason());
        lqw.eq(bo.getAuditorId() != null, MerMerchantApply::getAuditorId, bo.getAuditorId());
        return lqw;
    }

    /**
     * 新增商户申请
     *
     * @param bo 商户申请
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MerMerchantApplyBo bo) {
        MerMerchantApply add = MapstructUtils.convert(bo, MerMerchantApply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商户申请
     *
     * @param bo 商户申请
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MerMerchantApplyBo bo) {
        MerMerchantApply update = MapstructUtils.convert(bo, MerMerchantApply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MerMerchantApply entity) {
    }

    /**
     * 校验并批量删除商户申请信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
